import { createModel } from 'hox';
import { useState } from 'react';

import { getProjectList } from '@/services/common';

const SystemModel = () => {
  const [projectList, setProjectList] = useState({});

  async function fetchProjectList() {
    const { data } = await getProjectList();

    const projects = data?.project_list.map((v: { name: string; id: number }) => {
      return {
        ...v,
        label: v.name,
        value: v.id,
      };
    });

    setProjectList(projects);
    return projects;
  }

  return {
    projectList,
    fetchProjectList,
  };
};

export default createModel(SystemModel);
